package com.example.mychat.controller;

import com.example.mychat.annotation.GlobalInteceptor;
import com.example.mychat.entity.config.AppConfig;
import com.example.mychat.entity.constants.Constants;
import com.example.mychat.entity.po.AppUpdate;
import com.example.mychat.entity.vo.AppUpdateVO;
import com.example.mychat.entity.vo.ResponseVO;
import com.example.mychat.service.AppUpdateService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.Arrays;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/9
 */
@RestController
@RequestMapping("/update")
public class UpdateController extends ABaseController{

    @Resource
    private AppUpdateService appUpdateService;

    @Resource
    private AppConfig appConfig;

    @RequestMapping("/checkVersion")
    @GlobalInteceptor
    public ResponseVO checkVersion( String appVersion, String uid){
        AppUpdate latest = appUpdateService.getLatest(appVersion, uid);
        if (latest == null){
            return getSuccessResponseVO(null);
        }
        AppUpdateVO appUpdateVO = new AppUpdateVO();
        BeanUtils.copyProperties(latest,appUpdateVO);
        File file = new File(appConfig.getProjectFolder() + Constants.APP_UPDATE_FOLDER + latest.getId() + Constants.APP_EXE_SUFFIX);
        appUpdateVO.setSize(file.length());
        appUpdateVO.setUpdateList(Arrays.asList(latest.getUpdateDescArray()));
        String fileName = Constants.APP_NAME + latest.getVersion() + Constants.APP_EXE_SUFFIX;
        appUpdateVO.setFileName(fileName);
        return getSuccessResponseVO(appUpdateVO);
    }
}
